<?php
namespace Common\Logic\Common;

use Common\Base\LogicBase;
use Common\Model\Common\ZoneModel;

class ZoneLogic extends LogicBase
{
    public $m_zone;
    public $levels = array(
        1=>'省份',
        2=>'城市',
        3=>'区县',
        4=>'乡镇',
    );
    public $blank_select = '<select><option> - - 请选择 - - </option></select>';
    
    public function __construct() 
    {
        $this->m_zone = new ZoneModel();
    }
    
    /**
     * 得到地域列表
     *
     * @param int $w
     * @param array $search
     * @param string $fields
     * @param int $page
     * @param int $pageSize
     * @param string $by
     *
     * @return array
     */
    public function getZone($w=3, $search=null, $fields='*', $page=null, $pageSize=null, $by='')
    {
        return $this->m_zone->getZone($w, $search, $fields, $page, $pageSize, $by);
    }
    
    /**
     * 得到地域列表
     *
     * @param int   $id
     * @param array $result
     *
     * @return array
     */
    public function getUps($id=0, &$result)
    {
        $id = intval($id); if($id < 1){ return; }
        $info = $this->getZone(4, array('id'=>$id));
        if(empty($info)){ return; }
        $result[] = $info;
        $this->getUps($info['pid'], $result);
    }
    
    /**
     * 省份select
     *
     * @param int     $choose 默认选中
     * @param string  $name 名称、ID
     * @param string  $onchange 事件
     * @param boolean $disabled
     * @param string  $fst
     *
     * @return string
     */
    public function getProvinceSelect($choose=-1, $name='province_id', $onchange='', $disabled=false, $fst='--请选择--')
    {
        $dis = $disabled ? 'disabled="disabled"' : '';
        $choose = intval($choose);
        $html = '<select autocomplete="off" name="'.$name.'" id="'.$name.'" onchange="'.$onchange.'" '.$dis.'>';
        if(false !== $fst)
        {
            $html .= '<option value="-1">'.$fst.'</option>';
        }
        $datas = $this->getZone(2, array('pid'=>0));
        if( ! empty($datas))
        {
            foreach($datas as $v)
            {
                $v['id'] = intval($v['id']);
                $sel = ($v['id'] === $choose) ? 'selected="selected"' : '';
                $html .= '<option value="'.$v['id'].'" '.$sel.'>'.$v['area_name'].'</option>';
            }
        }
        $html .= '</select>';
        return $html;
    }
    
    /**
     * 城市select
     *
     * @param int     $province_id
     * @param int     $choose 默认选中
     * @param string  $name 名称、ID
     * @param string  $onchange 事件
     * @param boolean $disabled
     * @param string  $fst
     *
     * @return string
     */
    public function getCitySelect($province_id=-1, $choose=-1, $name='city_id', $onchange='', $disabled=false, $fst='--请选择--')
    {
        $province_id = intval($province_id);
        if($province_id < 1){ return $this->blank_select; }
        $dis = $disabled ? 'disabled="disabled"' : '';
        $choose = intval($choose);
        $html = '<select autocomplete="off" name="'.$name.'" id="'.$name.'" onchange="'.$onchange.'" '.$dis.'>';
        if(false !== $fst)
        {
            $html .= '<option value="-1">'.$fst.'</option>';
        }
        $datas = $this->getZone(2, array('pid'=>$province_id));
        if( ! empty($datas))
        {
            foreach($datas as $v)
            {
                $v['id'] = intval($v['id']);
                $sel = ($v['id'] === $choose) ? 'selected="selected"' : '';
                $html .= '<option value="'.$v['id'].'" '.$sel.'>'.$v['area_name'].'</option>';
            }
        }
        $html .= '</select>';
        return $html;
    }
    
    /**
     * 州县select
     *
     * @param int     $city_id
     * @param int     $choose 默认选中
     * @param string  $name 名称、ID
     * @param string  $onchange 事件
     * @param boolean $disabled
     * @param string  $fst
     *
     * @return string
     */
    public function getDistrictSelect($city_id=-1, $choose=-1, $name='district_id', $onchange='', $disabled=false, $fst='--请选择--')
    {
        $city_id = intval($city_id);
        if($city_id < 1){ return $this->blank_select; }
        $dis = $disabled ? 'disabled="disabled"' : '';
        $choose = intval($choose);
        $html = '<select autocomplete="off" name="'.$name.'" id="'.$name.'" onchange="'.$onchange.'" '.$dis.'>';
        if(false !== $fst)
        {
            $html .= '<option value="-1">'.$fst.'</option>';
        }
        $datas = $this->getZone(2, array('pid'=>$city_id));
        if( ! empty($datas))
        {
            foreach($datas as $v)
            {
                $v['id'] = intval($v['id']);
                $sel = ($v['id'] === $choose) ? 'selected="selected"' : '';
                $html .= '<option value="'.$v['id'].'" '.$sel.'>'.$v['area_name'].'</option>';
            }
        }
        $html .= '</select>';
        return $html;
    }
    
    /**
     * 乡镇select
     *
     * @param int     $district_id
     * @param int     $choose 默认选中
     * @param string  $name 名称、ID
     * @param string  $onchange 事件
     * @param boolean $disabled
     * @param string  $fst
     *
     * @return string
     */
    public function getTownSelect($district_id=-1, $choose=-1, $name='town_id', $onchange='', $disabled=false, $fst='--请选择--')
    {
        $district_id = intval($district_id);
        if($district_id < 1){ return $this->blank_select; }
        $dis = $disabled ? 'disabled="disabled"' : '';
        $choose = intval($choose);
        $html = '<select autocomplete="off" name="'.$name.'" id="'.$name.'" onchange="'.$onchange.'" '.$dis.'>';
        if(false !== $fst)
        {
            $html .= '<option value="-1">'.$fst.'</option>';
        }
        $datas = $this->getZone(2, array('pid'=>$district_id));
        if( ! empty($datas))
        {
            foreach($datas as $v)
            {
                $v['id'] = intval($v['id']);
                $sel = ($v['id'] === $choose) ? 'selected="selected"' : '';
                $html .= '<option value="'.$v['id'].'" '.$sel.'>'.$v['area_name'].'</option>';
            }
        }
        $html .= '</select>';
        return $html;
    }
}